﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Text;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        string strfullname = txtFullname.Text;
        string struser = txtUsername.Text;
        string strpass = txtPassword.Text;
        string strborn = txtBirthday.Text;
        string straddress = txtAddress.Text;
        bool gender = rbtmale.Checked = !rbtFemale.Checked; ;
        string stremail = txtEmail.Text;
        string strphone = txtPhone.Text;

        if (strpass.Length < 6 || strpass.Length > 18)
        {
            lblTB.Text = "Password must be between 6 and 18 characters";
            txtPassword.Focus();
            return;       
        }
        if (! ToolsDT.checkTel(strphone))
        {
            lblTB.Text = "phone number is invalid";
            txtPhone.Focus();
            return;
        }
        if (ToolsDT.exitsEmail(stremail))
        {
            lblTB.Text = "This e-mail is used, please use another email!";
            txtEmail.Focus();
            return;
        }
        if (ToolsDT.exitsUser(struser))
        {
            lblTB.Text = "This user already exists";
            txtUsername.Focus();
            return;
        }
         if (ToolsDT.exitsPhone(strphone))
        {
            lblTB.Text = "This phone number already exists";
            txtPhone.Focus();
            return;
        }
        
            SqlConnection cn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            try
            {
                cn = ToolsDT.getConnect();
                cmd.CommandText = "sp_Register";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@fullname", SqlDbType.VarChar,100).Value = strfullname;
                cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = struser;
                cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = strpass;
                cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = stremail;
                cmd.Parameters.Add("@mobile", SqlDbType.VarChar, 10).Value = strphone;
                cmd.Parameters.Add("@gender", SqlDbType.Bit).Value = gender;
                cmd.Parameters.Add("@dob", SqlDbType.DateTime).Value = strborn;
                cmd.Parameters.Add("@address", SqlDbType.VarChar, 200).Value = straddress;
                cmd.Connection = cn;
                cmd.ExecuteNonQuery();
                cn.Close();
                Response.Redirect("~/GUI/Prehome.aspx");            
            }
            catch(Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                cmd.Dispose();     
            }

    
    }

  
}
